/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgt.supermarket.controller.sales;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sgt.supermarket.entity.Employee;
import sgt.supermarket.entity.Invoice;
import sgt.supermarket.hibernatedao.HibernateDAOFactory;
import sgt.supermarket.util.ServletExtensions;

/**
 *
 * @author Administrator
 */
public class ViewOverallPerformance extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public class PerformanceRecord {

        private Employee employee;
        private double totalSales;

        public PerformanceRecord(Employee employee, double totalSales) {
            this.employee = employee;
            this.totalSales = totalSales;
        }

        /**
         * @return the employee
         */
        public Employee getEmployee() {
            return employee;
        }

        /**
         * @param employee the employee to set
         */
        public void setEmployee(Employee employee) {
            this.employee = employee;
        }

        /**
         * @return the totalSales
         */
        public double getTotalSales() {
            return totalSales;
        }

        /**
         * @param totalSales the totalSales to set
         */
        public void setTotalSales(double totalSales) {
            this.totalSales = totalSales;
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {

        HibernateDAOFactory factory = new HibernateDAOFactory();
        List<Date> dates = factory.getInvoiceDAO().getDistinctDates();
        request.setAttribute("dates", dates);
        if (request.getMethod().equalsIgnoreCase("post")) {
            Date selectedDate = ServletExtensions.getDateParam(request, "dateToView", "yyyy/MM/dd");
            List<Invoice> invoices = factory.getInvoiceDAO().getByDate(selectedDate);
            Map<Employee, List<Invoice>> map = new HashMap<Employee, List<Invoice>>();
            for (Invoice invoice : invoices) {
                if (map.get(invoice.getSalesperson()) == null) {
                    map.put(invoice.getSalesperson(), new ArrayList<Invoice>());
                }
                map.get(invoice.getSalesperson()).add(invoice);
            }
            List<PerformanceRecord> performanceRecords = new ArrayList<PerformanceRecord>();
            for (Employee e : map.keySet()) {
                double total = 0;
                for (Invoice invoice : map.get(e)) {
                    total += invoice.getTotalPrice();
                }
                performanceRecords.add(new PerformanceRecord(e, total));
            }
            request.setAttribute("selectedDate", selectedDate);
            request.setAttribute("performanceRecords", performanceRecords);
        }
        request.setAttribute("title", "View overall sales performance");
        request.getRequestDispatcher("/sales.viewOverallPerformance.tiles").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(ViewOverallPerformance.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(ViewOverallPerformance.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
